package project.Dao;/*
 *  @Author Mr.long
 *  @QQ 1507468405
 *  @Creat 2021/12/8 - 9:39
 */

import Utils.JDBCTemp;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import project.Domain.Contract;

import java.util.ArrayList;
import java.util.List;

public class ContractDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCTemp.getDataSource());


    public boolean insert(Contract c) {
        String sql = "insert into xm05_contract_table(boss,mobileA,employee,mobileB,contractTitle,contractFile," +
                "signedTime) values(?,?,?,?,?,?,?)" ;
        int update = template.update(sql, c.getBoss(), c.getMobileA(), c.getEmployee(), c.getMobileB(), c.getContractTitle(),
                c.getContractFile(), c.getSignedTime());

        return update > 0;
    }

    public boolean deleteByID(int id) {
        String sql = "delete from xm05_contract_table where id = ?";
        int update = template.update(sql, id);
        return update > 0;
    }

    public List<Contract> queryAll(int id, String role) {
        String sql = "select * from xm05_contract_table where mobileA = ?";
        List<Contract> list = new ArrayList<>();
        switch (role){
            case "管理员":
                 list = template.query(sql, new BeanPropertyRowMapper<Contract>(Contract.class),
                        new UserDao().getMobileByID(id));
                break;
            case "组长":
                String b = "select * from xm05_contract_table where mobileB = ?";
                list = template.query(b, new BeanPropertyRowMapper<Contract>(Contract.class),
                        new UserDao().getMobileByID(id));
                break;
        }
        return list;
    }
}
